package com.east.service.sale.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.east.bean.Contract;
import com.east.dao.ContractMapper;
import com.east.dao.ContractPayMapper;
import com.east.dao.InvoiceMapper;
import com.east.service.sale.ContractService;
import com.east.utils.DateUtils;
import com.east.utils.Pagenation;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Service("contractService")
public class ContractServiceImpl implements ContractService{
	@Autowired
	ContractMapper contractMapper;
	@Autowired
	InvoiceMapper invoiceMapper;
	@Autowired
	ContractPayMapper contractPayMapper;

	@Override
	public Page<Contract> findByPage(Pagenation pagenation, String key,Integer type,String startTime ,String endTime) {
		Page<Contract> startPage = PageHelper.startPage(pagenation.getPageNum(), pagenation.getPageSize());
		String ser="WXKJXM";
		if(type==27) {
			ser="WXKJ";
		}
		if(StringUtils.isNotBlank(startTime)) {
			startTime=ser+startTime.replace("-", "");
		}
		if(StringUtils.isNotBlank(endTime)) {
			endTime=ser+endTime.replace("-", "");
		}else {
			String daysOfDate = DateUtils.getDaysOfDate(new Date());
			endTime=ser+daysOfDate.replace("-", "");
		}
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("key", key);
		map.put("type", type);
		map.put("startTime", startTime);
		map.put("endTime", endTime);
		contractMapper.findAll(map);
		return startPage;
	}

	@Override
	public Contract findByOne(Contract contract) {
		return contractMapper.findOne(contract.getcId());
	}

	@Override
	public boolean saveContract(Contract contract) {
		Contract ct = new Contract();
		ct.setcAmount(contract.getcAmount());//合同金额
		ct.setcBalanceAmount(contract.getcBalanceAmount());//合同结余未付款
		ct.setcCaigou(contract.getcCaigou());//采购内容
		ct.setcCycle(contract.getcCycle());//质保周期/维护周期
		ct.setcForecost(contract.getcForecost());//合同决算金额
		ct.setcIfCompletePlan(contract.getcIfCompletePlan());//是否完成收款【字段只对销售合同有效】,默认否
		ct.setcIfPayInvoice(contract.getcIfPayInvoice());//是否开票
		ct.setcIsOver(contract.getcIsOver());//是否结束
		ct.setcKind(contract.getcKind());//合同类型[【字段只对销售合同有效】
		ct.setcMoney(contract.getcMoney());//付款时间
		ct.setcType(contract.getcType());
		ct.setcName(contract.getcName());//合同名
		ct.setCompanyPecificId(contract.getCompanyPecificId());//合同甲方公司id
		ct.setCompanySupplierId(contract.getCompanySupplierId());//合同乙方公司id
		ct.setcPaymentPlan(contract.getcPaymentPlan());//收款计划【字段只对销售合同有效】
		ct.setcPecific(contract.getcPecific());//合同甲方
		ct.setcRemark(contract.getcRemark());//合同备注
		ct.setcSerialNum(contract.getcSerialNum());//合同编号
		if(StringUtils.isNoneBlank(contract.getDateTime())) {
			ct.setcSignTime(DateUtils.getDate(contract.getDateTime()));//合同签订日期
		}
		if(StringUtils.isNoneBlank(contract.getPaymentTime())) {
			ct.setcMoney(DateUtils.getDate(contract.getPaymentTime()));//合同付款日期
		}
		ct.setcSerialNum(contract.getcSerialNum());//合同编号
		ct.setcSignTime(new Date());//合同签订日期
		ct.setcSupplier(contract.getcSupplier());//合同乙方
		ct.setcText(contract.getcText());//合同类型 [字典]
		ct.setcUuid(UUID.randomUUID().toString());//
		ct.setProjectId(contract.getProjectId());//项目id
		int insert = contractMapper.insert(ct);
		if(insert>0) {
			return true;
		}
		return false;
	}

	@Override
	public boolean updateContract(Contract contract) {
		Contract ct = contractMapper.selectByPrimaryKey(contract.getcId());
		ct.setcAmount(contract.getcAmount());//合同金额
		ct.setcBalanceAmount(contract.getcBalanceAmount());//合同结余未付款
		ct.setcCaigou(contract.getcCaigou());//采购内容
		ct.setcCycle(contract.getcCycle());//质保周期/维护周期
		ct.setcForecost(contract.getcForecost());//合同决算金额
		ct.setcIfCompletePlan(contract.getcIfCompletePlan());//是否完成收款【字段只对销售合同有效】,默认否
		ct.setcIfPayInvoice(contract.getcIfPayInvoice());//是否开票
		ct.setcIsOver(contract.getcIsOver());//是否结束
		ct.setcKind(contract.getcKind());//合同类型[【字段只对销售合同有效】
		ct.setcType(contract.getcType());
		ct.setcMoney(contract.getcMoney());//付款时间
		ct.setcName(contract.getcName());//合同名
		ct.setCompanyPecificId(contract.getCompanyPecificId());//合同甲方公司id
		ct.setCompanySupplierId(contract.getCompanySupplierId());//合同乙方公司id
		ct.setcPaymentPlan(contract.getcPaymentPlan());//收款计划【字段只对销售合同有效】
		ct.setcPecific(contract.getcPecific());//合同甲方
		ct.setcRemark(contract.getcRemark());//合同备注
		ct.setcSerialNum(contract.getcSerialNum());//合同编号
		if(StringUtils.isNoneBlank(contract.getDateTime())) {
			ct.setcSignTime(DateUtils.getDate(contract.getDateTime()));//合同签订日期
		}
		if(StringUtils.isNoneBlank(contract.getPaymentTime())) {
			ct.setcMoney(DateUtils.getDate(contract.getPaymentTime()));//合同付款日期
		}
		ct.setcSupplier(contract.getcSupplier());//合同乙方
		ct.setcText(contract.getcText());//合同类型 [字典]
		ct.setProjectId(contract.getProjectId());//项目id
		int insert = contractMapper.updateByPrimaryKey(ct);
		if(insert>0) {
			return true;
		}
		return false;
	}

	@Override
	@Transactional
	public boolean delContract(Contract contract) {
		contractPayMapper.delContract(contract.getcId());
		 invoiceMapper.delContract(contract.getcId());
		int deleteByPrimaryKey = contractMapper.deleteByPrimaryKey(contract.getcId());
		if(deleteByPrimaryKey>0) {
			return true;
		}
		return false;
	}
	@Override
	public int findByLike(Contract contract) {
		return contractMapper.findBySerNumLike(contract.getcSerialNum());
	}
	
}
